/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.alvin.auto.ui;

import org.alvin.auto.service.WordJacobService;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.alvin.auto.service.ExcelJacobService;

/**
 *
 * @author tangzhichao
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        setTitle("选择题自动阅卷工具");
        setResizable(false);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton4 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        paperDir = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        score = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        anwser = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        answerDocText = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        startRow = new javax.swing.JTextField();
        nameCol = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        scroeCol = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        excelPath = new javax.swing.JTextField();
        jCheckBox2 = new javax.swing.JCheckBox();
        jLabel8 = new javax.swing.JLabel();
        paperScoreCol = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        console = new javax.swing.JTextArea();

        jButton4.setText("选择");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel3.setText("试卷目录：");

        paperDir.setEditable(false);

        jButton1.setText("浏览");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("单题分数：");

        score.setText("2");

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("自动保存控制台信息");

        jButton2.setText("开始阅卷");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("正确答案：");

        anwser.setColumns(20);
        anwser.setRows(5);
        jScrollPane1.setViewportView(anwser);

        jLabel10.setText("答案文件：");

        answerDocText.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel10))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(score, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1)
                    .addComponent(answerDocText))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel10, jLabel2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(answerDocText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(score, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jButton2)
                    .addComponent(jCheckBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(21, 21, 21))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {answerDocText, jButton2, jCheckBox1, jLabel1, jLabel10, jLabel2, score});

        jTabbedPane2.addTab("自动改卷", jPanel1);

        jLabel4.setText("起始行数：");

        jLabel5.setText("姓名列数：");

        startRow.setText("5");

        nameCol.setText("2");

        jLabel6.setText("总分列数：");

        scroeCol.setText("3");

        jButton3.setText("开始总结");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel7.setText("总结文档：");

        excelPath.setEditable(false);

        jCheckBox2.setSelected(true);
        jCheckBox2.setText("保存控制台");

        jLabel8.setText("试卷总分所在列：");

        paperScoreCol.setText("4");

        jLabel9.setText("试卷分数表格中总分的列数");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(paperScoreCol, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(startRow, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jCheckBox2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton3))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(nameCol, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(38, 38, 38)
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(scroeCol, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(excelPath, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(71, 71, 71))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(excelPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(startRow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(nameCol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(scroeCol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel8)
                    .addComponent(paperScoreCol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(3, 3, 3)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jCheckBox2))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel4, jLabel5, jLabel6, nameCol, scroeCol, startRow});

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel8, jLabel9, paperScoreCol});

        jTabbedPane2.addTab("自动总结", jPanel2);

        console.setColumns(20);
        console.setRows(5);
        jScrollPane2.setViewportView(console);

        jTabbedPane1.addTab("控制台", jScrollPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(12, 12, 12)
                        .addComponent(paperDir, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 664, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(paperDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogType(JFileChooser.OPEN_DIALOG);
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.showOpenDialog(this);
        File f = jfc.getSelectedFile();
        if (f != null) {
            this.paperDir.setText(f.getAbsolutePath());
            autoSetPath(f);

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int res = JOptionPane.showConfirmDialog(null, "1.请先备份需要检查的文件\n"
                + "2.确认所有的 word 程序已经关闭"
                + "\n3.过程中也不得打开word"
                + "\n4.以上三点都确认以后，点击【确定】", "提示", JOptionPane.OK_CANCEL_OPTION);
        if (res != JOptionPane.OK_OPTION) {
            return;
        }
        if (this.paperDir.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请选择试卷目录！");
            this.paperDir.requestFocus();
            return;
        }
        try {
            Double.parseDouble(this.score.getText().trim());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "选择题分数请输入正确！");
            this.score.requestFocus();
            return;
        }
//        if (this.anwser.getText().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(null, "参考答案不能为空！");
//            this.score.requestFocus();
//            return;
//        }
        this.console.setText("");
        new Thread() {
            @Override
            public void run() {
                jButton2.setEnabled(false);
                doRun();
                if (jCheckBox1.isSelected()) {
                    try {
                        saveConsole(console.getText());
                    } catch (IOException ex) {
                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                jButton2.setEnabled(true);
            }

        }.start();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int res = JOptionPane.showConfirmDialog(null, "1.请先备份需要检查的文件\n"
                + "2.确认所有的 excel 程序已经关闭"
                + "\n3.过程中也不得打开 excel"
                + "\n4.确认已经试卷已经改完并已经正确的填写了总分"
                + "\n5.以上四点都确认以后，点击【确定】", "提示", JOptionPane.OK_CANCEL_OPTION);
        if (res != JOptionPane.OK_OPTION) {
            return;
        }
        if (this.paperDir.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请选择试卷目录！");
            this.paperDir.requestFocus();
            return;
        }
        if (this.excelPath.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请选择总结文档路径！");
            this.excelPath.requestFocus();
            return;
        }
        if (!this.startRow.getText().trim().matches("\\d+")) {
            JOptionPane.showMessageDialog(null, "请输入总结填写的起始行！");
            this.startRow.requestFocus();
            return;
        }
        if (!this.nameCol.getText().trim().matches("\\d+")) {
            JOptionPane.showMessageDialog(null, "请输入总结姓名列！");
            this.nameCol.requestFocus();
            return;
        }
        if (!this.scroeCol.getText().trim().matches("\\d+")) {
            JOptionPane.showMessageDialog(null, "请输入总结分数列！");
            this.scroeCol.requestFocus();
            return;
        }
        if (!this.paperScoreCol.getText().trim().matches("\\d+")) {
            JOptionPane.showMessageDialog(null, "请输入总结分数列！");
            this.paperScoreCol.requestFocus();
            return;
        }
        this.console.setText("");
        new Thread() {
            @Override
            public void run() {
                jButton3.setEnabled(false);
                autoValuation();
                if (jCheckBox1.isSelected()) {
                    try {
                        saveConsole(console.getText());
                    } catch (IOException ex) {
                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                jButton3.setEnabled(true);
            }

        }.start();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        JFileChooser jfc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel File", "xls", "xlsx");
        jfc.setFileFilter(filter);
        jfc.showOpenDialog(this);
        File f = jfc.getSelectedFile();
        if (f != null) {
            this.excelPath.setText(f.getAbsolutePath());
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField answerDocText;
    private javax.swing.JTextArea anwser;
    private javax.swing.JTextArea console;
    private javax.swing.JTextField excelPath;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField nameCol;
    private javax.swing.JTextField paperDir;
    private javax.swing.JTextField paperScoreCol;
    private javax.swing.JTextField score;
    private javax.swing.JTextField scroeCol;
    private javax.swing.JTextField startRow;
    // End of variables declaration//GEN-END:variables
    private void saveConsole(String text) throws IOException {
        String date = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        try (OutputStream out = Files.newOutputStream(Paths.get(this.paperDir.getText(), date.concat(".txt")))) {
            out.write(text.getBytes());
        }
    }

    private void doRun() {
        if (this.anwser.getText().trim().isEmpty()) {
            this.console.append("没有填写参考答案，自动获取中...\n");
            try (WordJacobService jacob = new WordJacobService()) {
                String answer = jacob.getAnswer(this.answerDocText.getText().trim(), this.console, 0);
                if (answer == null || answer.trim().isEmpty()) {
                    throw new Exception();
                }
                this.anwser.setText(answer);
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "自动获取答案出错，请手动输入");
                return;
            }
        }
        String[] answers = this.anwser.getText().trim().split("\t|\\s|[^A-Za-z]");
        List<String> answerList = new ArrayList<>();
        double score = Double.parseDouble(this.score.getText().trim());
        for (String s : answers) {
            answerList.add(s.replaceAll("[^A-Za-z]", "").toUpperCase());
        }
        String text = String.join(",", answerList);
        this.console.append("正确答案：\n");
        this.console.append(text + "\n");
        this.console.append("开始阅卷\n");
        File f = new File(this.paperDir.getText().trim());
        if (!f.isDirectory()) {
            JOptionPane.showMessageDialog(null, "您选择的不是目录！");
            return;
        }
        doAutoRun(f, answerList, score);

    }

    private void doAutoRun(File f, List<String> answerList, double score) {
        LinkedList<File> list = new LinkedList<File>();
        list.add(f);
        while (!list.isEmpty()) {
            File tmpF = list.removeFirst();
            if (tmpF.isDirectory()) {
                for (File subF : tmpF.listFiles()) {
                    list.add(subF);
                }
                continue;
            }
            if (tmpF.getName().toLowerCase().endsWith(".doc") || tmpF.getName().toLowerCase().endsWith(".docx")) {
                try {
                    check(tmpF, answerList, score);
                    this.console.append("\n----------------------------分割线----------------------------\n");
                } catch (Exception ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void check(File paperFile, List<String> answerList, double score) throws Exception {
        this.console.append("检查试卷：" + paperFile.getAbsolutePath() + "\n");
        try (WordJacobService jacob = new WordJacobService()) {
            jacob.check(paperFile, answerList, score, this.console);
        }

    }

    private void autoValuation() {
        this.console.append("要总结的目录：" + this.paperDir.getText() + "\n");
        this.console.append("总结文件：" + this.excelPath.getText() + "\n");
        this.console.append("开始总结\n");
        File f = new File(this.paperDir.getText().trim());
        if (!f.isDirectory()) {
            JOptionPane.showMessageDialog(null, "您选择的不是目录！");
            return;
        }
        File exclFile = new File(this.excelPath.getText().trim());
        if (!exclFile.exists()) {
            JOptionPane.showMessageDialog(null, "excel 文件不存在！");
            return;
        }
        Map<String, String> map = new LinkedHashMap<>();
        LinkedList<File> list = new LinkedList<File>();
        list.add(f);
        while (!list.isEmpty()) {
            File tmpF = list.removeFirst();
            if (tmpF.isDirectory()) {
                for (File subF : tmpF.listFiles()) {
                    list.add(subF);
                }
                continue;
            }

            if (tmpF.getName().toLowerCase().endsWith(".doc") || tmpF.getName().toLowerCase().endsWith(".docx")) {
                if (tmpF.getName().startsWith("~")) {
                    continue;
                }
                this.console.append("文件：" + tmpF.getAbsolutePath() + "\t");
                try {
                    try (WordJacobService jacob = new WordJacobService()) {
                        int col = Integer.parseInt(paperScoreCol.getText().trim());
                        String total = jacob.getTotalScroe(tmpF, col);
                        String name = tmpF.getName().toLowerCase();
                        name = name.substring(0, name.lastIndexOf("."));
                        map.put(name, total);
                        this.console.append("总分：" + total);
                    } catch (IOException ex) {
                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    this.console.append("\n----------------------------分割线----------------------------\n");
                } catch (Exception ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.console.append("\n");
            }
        }

        try (ExcelJacobService jacob = new ExcelJacobService()) {
            String baseDir = this.paperDir.getText().trim();
            int startRowInt = Integer.valueOf(this.startRow.getText().trim());
            int nameColInt = Integer.valueOf(this.nameCol.getText().trim());
            int scroeColInt = Integer.valueOf(this.scroeCol.getText().trim());
            String path = this.excelPath.getText().trim();
            jacob.valuation(baseDir, startRowInt, nameColInt, scroeColInt, map, path);
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void autoSetPath(File f) {
        File baseFile = f.getParentFile();
        String path = baseFile.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".doc") || name.toLowerCase().endsWith("docx");
            }
        })[0].getAbsolutePath();
        this.answerDocText.setText(path);

        path = baseFile.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".xls") || name.toLowerCase().endsWith("xlsx");
            }
        })[0].getAbsolutePath();
        this.excelPath.setText(path);
    }

}
